package com.wwz.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wwz.pojo.Personnel;
import com.wwz.pojo.model.LoginModel;
import com.wwz.pojo.query.PersonnelQuery;
import com.wwz.service.IPersonnelService;
import com.wwz.util.EcoBootException;
import com.wwz.util.JobUtils;
import com.wwz.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 人事管理控制层
 * @date 2022/03/24 31:02:10
 */
@RestController
@RequestMapping("/manage/personnel")
@Api(tags = "人事管理控制层")
public class PersonnelController {

    @Resource
    private IPersonnelService personnelService;

    @ApiOperation("人事登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginModel loginModel){
        JobUtils.checkLoginModel(loginModel);
        LoginModel login = personnelService.login(loginModel);
        if (login != null ){
            return Result.ok("ok",login);
        }
        return Result.error("用户名密码错误");
    }

    @ApiOperation("是否登录")
    @PostMapping("/isLogin")
    public Result isLogin(){
        StpUtil.checkLogin();
        return Result.ok();
    }

    @ApiOperation("退出登录")
    @PostMapping("/loginOut")
    public Result loginOut( ){
        StpUtil.logout();
        return Result.ok();
    }


    @ApiOperation("人事列表")
    @GetMapping("/toPage")
    public Result toPage(PersonnelQuery query){
        IPage<Personnel> personnelIPage = personnelService.toPage(query);
        return Result.ok(personnelIPage);
    }

    @ApiOperation("添加或修改人事信息")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Personnel personnel){
        if (StrUtil.isAllEmpty(personnel.getName(),personnel.getJobNumber(),personnel.getPassWord())){
            throw new EcoBootException("信息不能为空");
        }
        personnelService.addOrUpdate(personnel);
        return Result.ok("ok");
    }


    @ApiOperation("删除人事信息")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        personnelService.removeById(id);
        return Result.ok("ok");
    }

}
